/**
 * Created by zouyao on 2017/3/18.
 */
var m = angular.module("mainapp",[]);
m.controller("maincontroller",function($scope) {

    /**
     * 变量初始化
     * @type {[*]}
     */
    $scope.afterLoginStart = new Array();

    $scope.enlistConstant = ["报名成功后请于下月7日至15日查看准考证信息"];

    $scope.typeArraySelect = "-1";
    $scope.typeArray = ["java","android","ios","ui","web"];
    $scope.username = "";
    $scope.sex = "man";
    $scope.origin = "";
    $scope.graduate = "";
    $scope.major = "";
    $scope.tel = "";
    $scope.telFormatError = false;
    $scope.picture = "";

    $scope.password = "";
    $scope.confirmPwd = "";
    $scope.pwdLengthError = false;
    $scope.pwdFormatError = false;

    $scope.login_account = "";
    $scope.login_password = "";

    $scope.showStatus = 0;
    /**
     * 视图切换
     * 0 home
     * 1 login
     * 2 readBefore
     * 3 type
     * 4 infoDetail
     * 5 password
     * 6 pay
     * 7 successNum
     * 8 exam
     * 9 result
     */

    $scope.loginDiv = function () {
        $scope.showStatus = 1;
    };

    $scope.backToHomeDiv = function () {
        //$scope.showStatus = 0;
        window.location.href = "/jsp/enlist/index.jsp";
    };

    $scope.readBeforeDiv = function () {
        $scope.showStatus = 2;
    };

    $scope.acceptProtocol = function ($event) {
        //console.log($event.target.value);
        $scope.acceptProtocolStatus = $event.target.value;
    };
    
    $scope.readBeforeNext = function () {
        $scope.showStatus = 3;
    }

    $scope.typePrev = function () {
        $scope.showStatus = 2;
    };

    $scope.typeNext = function () {
        $scope.showStatus = 4;
    };

    $scope.infoDetailPrev = function () {
        $scope.showStatus = 3;
    };

    $scope.infoDetailNext = function () {
        $scope.showStatus = 5;
    };

    $scope.passwordPrev = function () {
        $scope.showStatus = 4;
    };

    $scope.passwordNext = function () {
        $scope.showStatus = 6;
    };

    $scope.payPrev = function () {
        $scope.showStatus = 5;
    };

    $scope.payDone = function () {
        $scope.showStatus = 7;
        $scope.signup();
    };

    /**
     * 登录
     */
    $scope.login = function () {
        //$scope.showStatus = 9;
        this.password = hex_md5($scope.login_password);
        $.ajax({
            type:"POST",
            url:"/user/getOneUser",
            data:{
                "account":$scope.login_account,
                "password":this.password
            },
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.afterLoginStart = new Array();
                $scope.$apply(function(){
                    if(data.message == '失败'){
                        alert("注册号或密码不正确!");
                    }else if(data.message == '获取成功'){
                        var obj = {};
                        obj['id'] = data.value.id;
                        obj['account'] = data.value.account;
                        obj['afterLogin'] = data.value.afterLogin;
                        obj['enlist'] = data.value.enlist;
                        obj['exam'] = data.value.exam;
                        var datestr = new Date(parseInt(data.value.createTime));
                        var temstr = datestr.getFullYear() + "年" + (parseInt(datestr.getMonth())+1) + "月" + datestr.getDate() + "日"
                            //+ datestr.getHours() + ":" + datestr.getMinutes() + ":" + datestr.getSeconds()
                            ;
                        obj['createTime'] = temstr;	//注册时间
                        $scope.afterLoginStart.push(obj);obj = {};
                        if($scope.afterLoginStart[0].afterLogin == 'new'){//可以修改注册信息
                            $scope.typeArraySelect = $scope.afterLoginStart[0].enlist.type;
                            $scope.username = $scope.afterLoginStart[0].enlist.username;;
                            $scope.sex = $scope.afterLoginStart[0].enlist.sex;
                            $scope.origin = $scope.afterLoginStart[0].enlist.origin;
                            $scope.graduate = $scope.afterLoginStart[0].enlist.graduate;
                            $scope.major = $scope.afterLoginStart[0].enlist.major;
                            $scope.tel = $scope.afterLoginStart[0].enlist.tel;
                            $scope.picture = $scope.afterLoginStart[0].enlist.picture;
                            $scope.ifShowPic = true;

                            $scope.password = "";
                            $scope.confirmPwd = "";

                            $scope.showStatus = 3;//调到报考类别页面
                        }else if($scope.afterLoginStart[0].afterLogin == 'exam'){//查看考试信息
                            $scope.examInfo = $scope.afterLoginStart[0].exam;
                            $scope.examInfo.typeArraySelect = $scope.afterLoginStart[0].enlist.type;
                            $scope.examInfo.username = $scope.afterLoginStart[0].enlist.username;
                            var sexTemp = $scope.afterLoginStart[0].enlist.sex;
                            $scope.examInfo.sex = sexTemp=='man'?"男":"女";
                            var datestr_ = new Date(parseInt($scope.afterLoginStart[0].exam.examTime));
                            var temstr_ = datestr_.getFullYear() + "年" + (parseInt(datestr_.getMonth())+1) + "月" + datestr_.getDate() + "日"
                                //+ datestr_.getHours() + ":" + datestr_.getMinutes() + ":" + datestr_.getSeconds()
                                ;
                            $scope.examInfo.examTime = temstr_;	//注册时间
                            $scope.showStatus = 8;
                        }else if($scope.afterLoginStart[0].afterLogin == 'result'){//查看考试结果
                            $scope.examInfo = $scope.afterLoginStart[0].exam;
                            $scope.examInfo.username = $scope.afterLoginStart[0].enlist.username;
                            $scope.showStatus = 9;
                        }
                    }
                });
            }
        });
    };

    /**
     * 变量绑定
     */
    $scope.typeArrayFun = function (obj) {
        $scope.typeArraySelect = obj;
        console.log($scope.typeArraySelect);
    };

    $scope.sexChoose = function ($event) {
        $scope.sex = $event.target.value;
    };

    /**
     * 上传图片
     * @type {boolean}
     */
    $scope.ifShowPic = false;
    $scope.doUpload = function () {
        var formData = new FormData($( "#uploadForm" )[0]);
        var i = formData.entries();

        console.log(i.next()); // {done:false, value:["k1", "v1"]}
        console.log(i.next()); // {done:false, value:["k1", "v1"]}
        $.ajax({
            url: '/user/addUserAvatar' ,
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (returndata) {
                console.log(returndata);
                $("#imgid-picPreview").attr("src",pictureTemp);
                $scope.picture = pictureTemp;
                $scope.ifShowPic = true;
            },
            error: function (returndata) {
                console.log(returndata);
                $scope.ifShowPic = false;
            }
        });
    };
    /**
     * 注册
     */
    $scope.signup = function () {
        var Num="";
        for(var i=0;i<12;i++)
        {
            Num+=Math.floor(Math.random()*10);
        }
        $scope.number = Num;//准考证号
        $scope.password = hex_md5($scope.password);
        $.ajax({
            type:"POST",
            url:"/user/addOneUser",
            data:{
                "password":$scope.password,
                "type":$scope.typeArraySelect,
                "username":$scope.username,
                "sex":$scope.sex,
                "origin":$scope.origin,
                "graduate":$scope.graduate,
                "major":$scope.major,
                "tel":$scope.tel,
                "picture":$scope.picture,
                "number":$scope.number
            },
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){

                });
            }
        });
    };
    /**
     * 更新信息
     */
    $scope.update = function () {
        $scope.password = hex_md5($scope.password);
        $.ajax({
            type:"POST",
            url:"/user/updateOneUser",
            data:{
                "account":$scope.afterLoginStart[0].account,
                "password":$scope.password,
                "type":$scope.typeArraySelect,
                "username":$scope.username,
                "sex":$scope.sex,
                "origin":$scope.origin,
                "graduate":$scope.graduate,
                "major":$scope.major,
                "tel":$scope.tel,
                "picture":$scope.picture
            },
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    if(data.message == '更新成功'){
                        $scope.backToHomeDiv();
                    }

                });
            }
        });
    };
    /**
     * 手机号格式验证
     */
    $scope.telValidate = function (obj) {
        console.log(obj.length);
        if(obj.length != 11){
            $scope.telFormatError = true;
        }else if(obj.length == 11){
            $scope.telFormatError = false;
        }
    };
    /**
     * 密码长度验证
     */
    $scope.pwdLengthValidate = function (p) {
        if(p.length < 6){
            $scope.pwdLengthError = true;
        }else if(p.length >= 6){
            $scope.pwdLengthError = false;
        }
    };
    /**
     * 确认密码验证
     */
    $scope.pwdValidate = function (p1,p2) {
        if(p1 != p2){
            $scope.pwdFormatError = true;
        }else if(p1 == p2){
            $scope.pwdFormatError = false;
        }
    };

});

//上传照片
var pictureTemp = "";
var showPreview = function (obj) {//获取文件名

    pictureTemp = //$("#imgid-picPreview").attr("title")+
        "/avatar-img/"+
        obj.value.substring(obj.value.lastIndexOf("\\")+1);
    console.log($("#imgid-picPreview").attr("title"));
    console.log(pictureTemp);

};
